package net.niufei.packet;

import java.util.concurrent.atomic.AtomicLong;

import net.niufei.Packet;

public class PriorityPacket extends DelegatePacket implements
		Comparable<PriorityPacket> {
	private static final AtomicLong PRIORITY_SEQ = new AtomicLong(0);
	
	private final int priority;
	private final long seq;
	
	public PriorityPacket(Packet packet) {
		this(packet, 0);
	}
	
	
	public PriorityPacket(Packet packet, int priority) {
		super(packet);
		this.priority = priority;
		this.seq = PRIORITY_SEQ.getAndIncrement();
	}

	
	@Override
	public int compareTo(PriorityPacket packet) {
		int p = priority - packet.priority;
		return p == 0 ? (seq < packet.seq ? -1 : 1) : p; 
	}
	
	
	public String toString() {
		return super.toString() + " [priority] " + priority + " [sequence] "
				+ seq; 
	}
	
}
